import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
//引入css样式表
Vue.config.productionTip = false
const eventBus = new Vue();
Vue.prototype.eventBus = eventBus;
import axios, { Axios } from 'axios'
axios.defaults.baseURL = 'https://api.shop.eduwork.cn/'
//axios拦截器请求拦截
axios.interceptors.request.use(config => {
  const token = localStorage.getItem('token') || '';
  //把token设置到请求头中
  config.headers.Authorization = token
  //把信息返回出去
  return config
}, error => {
  console.log(error);
})
//响应拦截器
axios.interceptors.response.use(res => {
  console.log(res);
  // 数据请求成功后，对数据进行简化
  return res.data
}, error => {
  if (error.request) {
    switch (error.request.status) {
      case 401:
        // 如果状态码为401，就是未授权
        // 需要把token删除
        localStorage.removeItem('token');
        // 需要跳转到登录页面
        router.replace('/login')
        break;
      default:
        console.log('未知错误');
        router.replace('/login')
        break;
    }
  }
})
Vue.prototype.$axios = axios;
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')